package com.banban.user.controller;

import com.banban.user.dto.RegisterDTO;
import com.banban.user.dto.UpdateUserDTO;
import com.banban.user.dto.UserPageDTO;
import com.banban.user.exception.BusinessException;
import com.banban.user.service.UserService;
import com.banban.user.vo.DataResult;
import com.banban.user.vo.PromptTemplateVO;
import com.banban.user.vo.UserPageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/sys")
public class SysUserController {

    @Resource
    private UserService service;


    /**
     * 分页查询用户信息
     * @return
     */
    @PostMapping("/userPage")
    public DataResult userPage(@RequestBody UserPageDTO dto) {
        IPage<UserPageVO> vo = service.userPage(dto);
        return DataResult.success(vo);
    }

    /**
     * 根据用户id获取用户信息
     * @param dto
     * @return
     */
    @PostMapping("/getUserInfoById")
    public DataResult getUserInfoById(@RequestBody @Valid UpdateUserDTO dto) {
        UserPageVO vo = service.getUserInfoById(dto.getId());
        return DataResult.success(vo);
    }

    /**
     * 编辑用户信息
     * @param dto
     * @return
     */
    @PostMapping("/updateUserInfoById")
    public DataResult updateUserInfoById(@RequestBody @Valid UpdateUserDTO dto) {
        service.updateUserInfoById(dto);
        return DataResult.success();
    }

    /**
     * 解锁/锁定用户
     * @param dto
     * @return
     */
    @PostMapping("/lockUser")
    public DataResult lockUser(@RequestBody @Valid UpdateUserDTO dto) {
        if (dto.getStatus() == null){
            throw new BusinessException("状态不能为空");
        }
        service.lockUser(dto);
        return DataResult.success();
    }

    /**
     * 新增用户
     * @param dto
     * @return
     */
    @PostMapping("/create")
    public DataResult create(@RequestBody  RegisterDTO dto) {
        service.registerNew(dto);
        return DataResult.success();
    }

    /**
     * 删除用户
     * @param dto
     * @return
     */
    @PostMapping("/deleteUser")
    public DataResult deleteUser(@RequestBody @Valid UpdateUserDTO dto) {
        service.deleteUser(dto);
        return DataResult.success();
    }

}
